package com.dyz.leetcode._18_backtracking;

import java.util.ArrayList;
import java.util.List;

public class _131_partition {
    private String s;
    private List<List<String>> res;
    public List<List<String>> partition(String s) {
        this.s = s;
        this.res = new ArrayList<>();
        List<String> path = new ArrayList<>();
        dfs(0, path);
        return res;
    }
    private void dfs(int startIndex, List<String> path){
        if(startIndex==s.length()){
            res.add(new ArrayList<>(path));
            return;
        }

        for(int i=startIndex; i<s.length(); i++){
            int endIndx = i;
            if(!checkPalindrome(s, startIndex, endIndx)){continue;}
            path.add(s.substring(startIndex, endIndx+1));
            dfs(i+1, path);
            path.remove(path.size()-1);

        }
    }

    private boolean checkPalindrome(String s, int left, int right) {
        while (left<right){
            if(s.charAt(left)!=s.charAt(right)){return false;}
            left++;
            right--;
        }
        return true;

    }
}
